#!/usr/bin/env python

import pygame, sys, glob
from pygame import *

h=400
w=800

screen = pygame.display.set_mode((w,h))

clock=pygame.time.Clock()
ground = 300
gravity = 3

class player:
    global ground,gravity
    def __init__(self):
        self.x = 200
        self.y = 300
        self.ani_speed_init=10
        self.ani_speed=self.ani_speed_init
        self.ani = glob.glob("../img/doom_w*.png")
        self.state = 0 #1 for jumped state
        self.jump = 300 #increase for higher jump
	self.run = 3       
 
        self.ani.sort()
        self.ani_pos=0
        self.ani_max=len(self.ani)-1
        self.img = pygame.image.load(self.ani[0])
        self.update(0,0)
        
    
    #def img(self):
        #return self.img
    def getx(self) :
        return self.x

    def update(self, pos,dir):
        if pos != 0:
            self.ani_speed-=1
            self.x+=pos[0]*self.run
            if self.y < ground:
                #Gravity effect
                if (self.y + gravity) > ground:
                    self.y = ground
                else:
                    self.y += gravity
                
            else :
                self.y-=pos[1]*self.jump
                
            if self.ani_speed == 0:
                self.img = pygame.image.load(self.ani[self.ani_pos])
                if dir == 1: #right
                    self.img = pygame.transform.flip(self.img,True,False)
                self.ani_speed = self.ani_speed_init
                if self.ani_pos == self.ani_max:
                    self.ani_pos=0
                else:
                    self.ani_pos+=1
        print self.ani_pos
        screen.blit(self.img,(self.x,self.y))
           

class missile(pygame.sprite.Sprite):
    image = None
    
    def __init__(self, initial_position):
        pygame.sprite.Sprite.__init__(self)
        if missile.image is None:
            missile.image = pygame.image.load("../img/bulleta.bmp")
        self.image = missile.image

        self.rect = self.image.get_rect()
        self.rect.topleft = initial_position
        self.next_update_time = 0 # update() hasn't been called yet.
        
    def update(self, current_time, left, right):
        global missile
        # Update every 2 milliseconds = 1/500th of a second.
        if self.next_update_time < current_time:
            # If we're reached the top then stop
            if self.rect.topleft[1] == 0:
                missiles.remove(self)
                self.kill()
                return
            else:
                self.rect.topleft = [self.rect.topleft[0]+5, self.rect.topleft[1]]
            self.next_update_time = current_time + 4

player1 = player()
pos = [0,0]
dir = 0 #0 for left and 1 for right
missiles = []

while 1:
    screen.fill((0,0,0))
    clock.tick(100)
    time = pygame.time.get_ticks()
    x = player1.x
    print "your x coordinate: %d" % (x)
    
    for event in pygame.event.get():
       if event.type == pygame.QUIT:
           sys.exit()
       elif event.type == KEYDOWN and event.key == K_RIGHT:
           pos[0] = 1
           dir = 1
       elif event.type == KEYUP and event.key == K_RIGHT:
           pos[0] = 0
       elif event.type == KEYDOWN and event.key == K_LEFT:
           pos[0] = -1
           dir = 0
       elif event.type == KEYUP and event.key == K_LEFT:
           pos[0] = 0
       elif event.type == KEYDOWN and event.key == K_SPACE:
           missiles.append(missile([x, player1.y]))
       elif event.type == KEYDOWN and event.key == K_UP:
            pos[1] = 1
       elif event.type == KEYUP and event.key == K_UP:
            pos[1] = 0
        
    for m in missiles:
        m.update(time, 0, w)
        screen.blit(m.image, m.rect)
    
    player1.update(pos,dir)
    pygame.display.update()
